from django.db import connection

from libs.db import escape_fields


def get_storage_cards():
    arr = []
    try:
        cols = ['id', 'quota', 'dedu_ratio', 'month_ratio', 'created_at']
        escape_cols = escape_fields(cols)
        fields_str = ','.join(escape_cols)
        sql = 'select {fields_str} from storage_card' \
              ' where is_delete=0 order by `sort` asc'.format(fields_str=fields_str)
        with connection.cursor() as cursor:
            cursor.execute(sql)
            rows = cursor.fetchall()
            for row in rows:
                data = {}
                for i, field in enumerate(cols):
                    data[field] = row[i]
                arr.append(data)
    except Exception as e:
        print(e)
    return arr
